package com.imstlife.vending.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.imstlife.vending.R;

public class UpApkDialog extends Dialog implements View.OnClickListener {
    private static UpApkDialog lmld;
    private Context context;
    private TextView mqttLoginNum;
    private Button mqttLoginClear;
    private OkThread ot;
    private getMqttLogin gml;
    private TextView mqttlogintop;
    private int okNum = 5;
    private boolean showFlag = false;
    private Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mqttLoginNum.setText(msg.obj.toString());
        }
    };

    private UpApkDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        setView();
    }

    public static UpApkDialog getlmld(Context context) {
        if (lmld == null) {
            lmld = new UpApkDialog(context);
        }
        return lmld;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.liking_mqtt_login_clear:
                showFlag = false;
                dismiss();
                break;
        }
    }

    public void setInterface(getMqttLogin gml) {
        this.gml = gml;
    }

    private void setView() {
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_mqttlogin, null);
        mqttlogintop = (TextView) v.findViewById(R.id.mqttlogintop);
        mqttLoginNum = (TextView) v.findViewById(R.id.liking_mqtt_login_num);
        mqttLoginClear = (Button) v.findViewById(R.id.liking_mqtt_login_clear);
        mqttLoginClear.setOnClickListener(this);
        super.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setContentView(v);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        //window.setWindowAnimations(R.style.mystyle);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = 400;
        params.height = 350;
        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        window.setAttributes(params);
    }

    @Override
    public void show() {
        okNum = 5;
        ot = new OkThread();
        ot.start();
        super.show();
        showFlag = true;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        showFlag = false;
    }
    public interface getMqttLogin {
        void showApk();
    }

    public class OkThread extends Thread {
        @Override
        public void run() {
            while (true) {
                Message mes = Message.obtain();
                mes.obj = okNum + "";
                hand.sendMessage(mes);
                Log.e("recData", "daojishi==" + okNum);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (showFlag) {
                    okNum--;
                    if (okNum <= 0) {
                        dismiss();
                        gml.showApk();
                        return;
                    }
                } else {
                    return;
                }
            }
        }
    }
}
